[id].tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { Head } from "$fresh/runtime.ts";
  2. import { Handlers, PageProps } from "$fresh/server.ts";
  3. import { checkToken } from "utils/server.ts";
  4. import { find } from "utils/db.ts";
  5. import TopBar from "../islands/TopBar.tsx";
  6. import Editor, { EditorMode } from "../islands/Editor.tsx";
  7. interface PostProps {
  8. id: string;
  9. title: string;
  10. content: string;
  11. shared: boolean;
  12. isLogined: boolean;
  13. allowMode: EditorMode;
  14. }
  15. export const handler: Handlers<PostProps> = {
  16. GET(req, ctx) {
  17. const tokenUserId = checkToken(req);
  18. const postId = ctx.params.id;
  19. const post = find(
  20. "Post",
  21. tokenUserId
  22. ? {
  23. id: postId,
  24. user_id: tokenUserId,
  25. }
  26. : { id: postId, shared: true },
  27. ["title", "content", "shared"],
  28. );
  29. if (post.length > 0) {
  30. return ctx.render({
  31. id: postId,
  32. isLogined: Boolean(tokenUserId),
  33. allowMode: tokenUserId ? EditorMode.Both : EditorMode.Read,
  34. title: post[0][0] as string,
  35. content: post[0][1] as string,
  36. shared: post[0][2] as boolean,
  37. });
  38. }
  39. // Redirect to 404 page if not found
  40. return ctx.renderNotFound();
  41. },
  42. };
  43. export default function Post(props: PageProps) {
  44. return (
  45. <>
  46. <Head>
  47. <title>{props.data.title}</title>
  48. </Head>
  49. <div className="pd-page">
  50. <TopBar
  51. id={props.data.id}
  52. title={props.data.title}
  53. shared={props.data.shared}
  54. allowMode={props.data.allowMode}
  55. isLogined={props.data.isLogined}
  56. />
  57. <Editor
  58. id={props.data.id}
  59. content={props.data.content}
  60. allowMode={props.data.allowMode}
  61. />
  62. </div>
  63. </>
  64. );
  65. }